package com.l2jserver.gameserver.model.zone.type;

import com.l2jserver.gameserver.datatables.SkillTable;
import com.l2jserver.gameserver.model.L2Object.InstanceType;
import com.l2jserver.gameserver.model.L2Skill;
import com.l2jserver.gameserver.model.actor.L2Character;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.model.zone.L2SpawnZone;
import com.l2jserver.gameserver.network.SystemMessageId;
import com.l2jserver.gameserver.network.serverpackets.SystemMessage;

/**
 * An Custom PvP Zone
 *
 * @author  NeverMore
 */
public class L2TzCustomZone extends L2SpawnZone
{
	private boolean _isPeaceZone;
	private boolean _giveNobleBlessing;
	private boolean _removeNobleBlessing;
	private boolean _canSummonFriends;
	L2Skill _NoblesseBlessing;
	L2Skill _CelestialShield;
	private boolean _istownzone;
	
	public L2TzCustomZone(final int id)
	{
		super(id);
		setTargetType(InstanceType.L2Playable); // default only playabale
		
		_NoblesseBlessing = SkillTable.getInstance().getInfo(1323, 1);
		_CelestialShield = SkillTable.getInstance().getInfo(1418, 1);
		
		_isPeaceZone = false;
		_giveNobleBlessing = false;
		_removeNobleBlessing = false;
		_canSummonFriends = true;
		_istownzone = false;
	}
	
	@Override
	public void setParameter(final String name, final String value)
	{
		if (name.equals("isPeaceZone"))
			_isPeaceZone = Boolean.parseBoolean(value);
		else if (name.equals("AutoGiveNobles"))
			_giveNobleBlessing = Boolean.parseBoolean(value);
		else if (name.equals("AutoRemoveNobles"))
			_removeNobleBlessing = Boolean.parseBoolean(value);
		else if (name.equals("AllowSummonFriend"))
			_canSummonFriends = Boolean.parseBoolean(value);
		else
			super.setParameter(name, value);
	}
	
	@Override
	protected void onEnter(final L2Character character)
	{
		if (character instanceof L2PcInstance)
		{
			character.setInsideZone(L2Character.ZONE_TZ_CUSTOM, true);
			if (_isPeaceZone)
			{
				character.setInsideZone(L2Character.ZONE_PEACE, true);
				((L2PcInstance) character).sendPacket(SystemMessage.getSystemMessage(SystemMessageId.ENTER_PEACEFUL_ZONE));
			}
			if (!_canSummonFriends)
				character.setInsideZone(L2Character.ZONE_NOSUMMONFRIEND, true);
			if (character instanceof L2PcInstance)
			{
				final L2PcInstance activechar = (L2PcInstance) character;
				if (!activechar.isNoblesseBlessed() && _giveNobleBlessing)
					_NoblesseBlessing.getEffects(activechar, activechar);
			}
		}
	}
	
	@Override
	protected void onExit(final L2Character character)
	{
		if (character instanceof L2PcInstance)
		{
			character.setInsideZone(L2Character.ZONE_TZ_CUSTOM, false);
			// TODO: there should be no exit if there was possibly no enter
			if (_isPeaceZone)
			{
				character.setInsideZone(L2Character.ZONE_PEACE, false);
				((L2PcInstance) character).sendPacket(SystemMessage.getSystemMessage(SystemMessageId.EXIT_PEACEFUL_ZONE));
			}
			if (!_canSummonFriends)
				character.setInsideZone(L2Character.ZONE_NOSUMMONFRIEND, false);
			if (character instanceof L2PcInstance)
				if (((L2PcInstance) character).isNoblesseBlessed() && _giveNobleBlessing && _removeNobleBlessing)
					((L2PcInstance) character).stopNoblesseBlessing(null);
		}
	}
	
	@Override
	public void onDieInside(final L2Character character)
	{
	}
	
	@Override
	public void onReviveInside(final L2Character character)
	{
	}
}
